local zihui = fk.CreateSkill {
  name = "lz__zihui",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["lz__zihui"] = "恣毁",
  [":lz__zihui"] = "锁定技，你于一回合内首次：失去最后的手牌后，加1点体力上限并摸一张牌；回复体力至上限后，减1点体力上限并弃置一名角色一张牌。",

  ["#lz__xiabian-choose-target"] = "恣毁：选择一名角色，弃置其一张牌",
  ["#lz__xiabian-choose-card"] = "恣毁：选择弃置 %dest 的一张牌",
  -- ["$lz__zihui1"] = "",
  -- ["$lz__zihui2"] = "",
}

zihui:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zihui.name) and player:isAlive() and player:getMark(zihui.name .. "_h-turn") == 0 then
      for _, move in ipairs(data) do
        if move.from == player and table.find(move.moveInfo, function(info) return info.fromArea == Player.Hand end) and player:isKongcheng() then
          player.room:setPlayerMark(player, zihui.name .. "_h-turn", 1)
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    player:drawCards(1)
  end
})

zihui:addEffect(fk.HpChanged, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    if player:isAlive() and player:hasSkill(zihui.name) and player == target and data.num > 0 and player.hp == player.maxHp and player:getMark(zihui.name .. "_hp-turn") == 0 then
      player.room:setPlayerMark(player, zihui.name .. "_hp-turn", 1)
      return true
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -1)
    local tos = room:askToChoosePlayers(player, {
      targets = table.filter(room:getAlivePlayers(), function(p) return not p:isNude() end),
      min_num = 1,
      max_num = 1,
      prompt = "#lz__xiabian-choose-target",
      skill_name = zihui.name,
      cancelable = false
    })
    local to = tos[1]
    local cid = room:askToChooseCard(player,
      { target = to, flag = "he", skill_name = zihui.name, prompt = "#lz__xiabian-choose-card::" .. to.id })
    room:throwCard(cid, zihui.name, to, player)
  end,
})

return zihui
